x_list = [0.4, 0.55, 0.65, 0.8, 0.9]
y_list = [0.41075, 0.57815, 0.69675, 0.88811, 1.02652]


def get_value(n):
    val = 0
    for i in range(n + 1):
        temp = y_list[i]
        for j in range(n + 1):
            if i != j:
                temp /= (x_list[i] - x_list[j])
        val += temp
    return val


# 牛顿插值
def newton(target):
    res = 0
    for i in range(len(x_list)):
        temp = 1
        val = get_value(i)
        for j in range(i):
            temp = temp * (target - x_list[j])
        res += val * temp
    return res


def do_calculate():
    target = float(input("请输入插值点: "))
    print("插值点(x, y) = (", target, ",", newton(target), ")")


if __name__ == '__main__':
    do_calculate()
